﻿using System;
using Domain;
using System.Diagnostics;

namespace Presenter
{
    public class MergeSortPresenter : IPresenterCallbacks
    {
        private IView view;
        private ILogger logger;

        public MergeSortPresenter(IView view, ILogger logger)
        {
            this.view = view;
            this.logger = logger;
            view.Attach(this);
        }

        public void OnSort()
        {
            string inputArrayString = view.inputArray;
            
            if (ArrayFormatIsCorrect(inputArrayString))
            {
                int[] inputArray = ParseArrayString(inputArrayString);
                int[] outputArray = MergeSort.Sort(inputArray);
                string outputArrayString = MakeArrayString(outputArray);
                view.outputArray = outputArrayString;

                logger.Write(">Array is sorted");
                logger.Write(String.Format("From: {0}", inputArrayString));
                logger.Write(String.Format("To:   {0}", outputArrayString));
            }
            else
            {
                view.ShowMessageBox("Wrong array format (must be integers divided by single spaces)");
                view.outputArray = "";

                logger.Write(">Wrong array format");
                logger.Write(inputArrayString);
            }
        }

        private int[] ParseArrayString(string arrayString)
        {
            string[] parsedArrayString = arrayString.Split(' ');
            int arrayLength = parsedArrayString.Length;
            int[] intArray = new int[arrayLength];

            for (int i = 0; i < arrayLength; i++)
                intArray[i] = Convert.ToInt32(parsedArrayString[i]);

            return intArray;
        }

        private string MakeArrayString(int[] array)
        {
            string outputString = "";

            for (int i = 0; i < array.Length; i++)
            {
                outputString += array[i].ToString();
                if (i < (array.Length - 1))
                    outputString += " ";
            }

            return outputString;
        }

        private bool ArrayFormatIsCorrect(string arrayString)
        {
            char lastSymbol = ' ';
            if (arrayString == "")
                return false;
            if ((arrayString[0] == ' ') || (arrayString[arrayString.Length-1] == ' '))
                return false;

            try
            {
                for (int i = 0; i < arrayString.Length; i++)
                {
                    if (arrayString[i] == ' ')
                    {
                        if (lastSymbol == ' ')
                            return false;
                    }
                    else
                        Convert.ToInt32(arrayString[i].ToString());

                    lastSymbol = arrayString[i];
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public string GetLog()
        {
            return logger.Read();
        }

        public void ShowLog()
        {
            switch (view.selectedLogType)
            {
                case 1:
                    Process.Start(logger.logFile);
                    break;
                case 0:
                    view.ShowLog(logger.Read());
                    break;
            }    
        }

        public void LogTypeChanged()
        {
            switch (view.selectedLogType)
            {
                case 1:
                    logger = view.GetLoggerToHtml();
                    break;
                case 0:
                    logger = view.GetLoggerToTxt();
                    break;
            }           
        }
    }
}
